package com.situ.mall.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.situ.mall.admin.entity.User;
import com.situ.mall.admin.exception.MyException;
import com.situ.mall.admin.mapper.UserMapper;
import com.situ.mall.admin.service.IUserService;
import com.situ.mall.common.entity.Member;
import com.situ.mall.common.util.RequestUtil;
import com.situ.mall.common.util.StringUtil;
import com.situ.mall.common.vo.ResultVO;
import com.situ.mall.common.vo.TableVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 闫佳宁
 * @since 2023-09-11
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean check(String username, String password) {
        User u = baseMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username));
        boolean b = u != null && u.getPassword().equals(StringUtil.md5(password));
        if (b) {
//把当前用户保存进session
            RequestUtil.setCurrent(u);
        }
        return b;
    }


    @Override
    public ResultVO delete(Integer id) {
        List<User> list = userMapper.selectById(id);
        User u = list.get(0);
        ResultVO vo = new ResultVO();
        if (u.getUsername().equals("admin")) {
            vo.setCode(2);
            vo.setMsg("该账号为管理员账号，不能删除");
        } else {
            userMapper.delete(id);
        }
        return vo;
    }

    @Override
    public User info(Integer id) {
        List<User> list = userMapper.selectById(id);
        return list.size() > 0 ? list.get(0) : null;
    }

    @Override
    public TableVO select(Integer page, Integer limit, String username, String name, Integer status) {
        //pageHelper 分页小工具
        PageHelper.startPage(page, limit);
        //上面这行代码后跟的第一次查数据库是分页查询，只生效一次
        return TableVO.data(userMapper.select(username, name, status));
    }

    @Override
    public void del(String ids) {
        userMapper.del(ids.split(","));
    }

    @Override
    public TableVO UpdatePassword(String oldPassword, String newPassword, String newPasswordAgain, HttpServletRequest request, HttpServletResponse response) {
        TableVO vo = new TableVO();
        User user =(User)RequestUtil.getCurrent();
        if(!user.getPassword().equals(StringUtil.md5(oldPassword))){
            vo.setCode(2);
            vo.setMsg("原始密码输入错误");
            return vo;
        }
        if(!newPassword.equals(newPasswordAgain)){
            vo.setCode(2);
            vo.setMsg("两次输入输入不一致");
            return vo;
        }
        if (newPassword.length()>16 || newPassword.length()<6) {
            vo.setCode(2);
            vo.setMsg("修改失败，密码应在6-16位");
            return vo;
        }
        vo.setCode(0);
        vo.setMsg("密码修改成功");
        request.getSession().getAttribute("currentUser");
        userMapper.UpdatePassword(StringUtil.md5(newPassword),user.getId());
        return vo;
    }




}

